package 力扣_简单题;

//  Definition for singly-linked list.

import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}
//思路，新建一个链表、采用头插法把旧链表的节点插入即可
public class 反转链表 {
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newNode = new ListNode(0);
        ListNode temp = head;
        ListNode cur = head;
        while (cur != null) {
            temp = cur.next;//保存下一个节点
            cur.next = newNode.next;//将当前节点的下一个节点指向新链表头节点的下一个节点
            newNode.next = cur;//新链表的头节点指向这个节点
            cur = temp;//从temp中获取到旧链表的位置
        }
       /* while (newNode.next != null) {
            System.out.println(newNode);
            newNode = newNode.next;
        }*/
        return newNode.next;
    }
}
